package com.app.actions.client;

import com.app.SpringAnnotationDef;
import com.app.actions.BaseAction;
import com.app.model.User;
import com.app.service.client.ClientManagerService;
import com.app.utils.Pager;
import net.sf.json.JSONObject;
import com.app.model.ClientManager;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: cnprinces
 * Date: 13-1-21
 * Time: 下午4:35
 * To change this template use File | Settings | File Templates.
 */
@Scope(SpringAnnotationDef.SCOPE_PROTOTYPE)
@Component
@Namespace("/client")
public class ClientManagerAction extends BaseAction {
    @Resource(name = SpringAnnotationDef.CLIENTMANAGER_SERVICE)
    private ClientManagerService service;


    private ClientManager clientManager;


    public ClientManager getClientManager() {
        return clientManager;
    }

    public void setClientManager(ClientManager clientManager) {
        this.clientManager = clientManager;
    }

    //actions

    @Action(value = "client", results = {@Result(name = SUCCESS, location = "/client/list.jsp"),
            @Result(name = "addClientManager", location = "/client/addClientManager.jsp")
    })
    public String getClientManagerList() {
        /*List<ClientManager> list = service.getDao().listAll();*/
        String pageNum = getRequest().getParameter("pageNum");
        String numPerPage = getRequest().getParameter("numPerPage");
        String orderField = getRequest().getParameter("orderField");
        if (StringUtils.isNotBlank(numPerPage)) {
            pager.setPageNumber(Integer.parseInt(pageNum));
            pager.setPageSize(Integer.parseInt(numPerPage));
          /*  if(pager.getOrder()==null){
                pager.setOrder(Pager.Order.asc);
            }else{
                if(pager.get.order.toString().equals(Pager.Order.asc.toString())){
                    pager.setOrder(Pager.Order.desc);
                } else{
                    pager.setOrder(Pager.Order.asc);
                }
            }*/

            pager.setOrderBy(orderField);
        }

        pager = service.getDao().pagerList(pager);
        /*getRequest().setAttribute("list", list);*/

        return SUCCESS;
    }


    public String toAdd() {

        if (clientManager != null && clientManager.getId() != null) {
            clientManager = service.getDao().get(clientManager.getId());
            getRequest().setAttribute("clientManager", clientManager);
        }
        return "addClientManager";
    }


    public void addClientManager() {
        if (clientManager != null) {
            User user = getSessionLoginedUser();
            clientManager.setCreateDate(new Date());
            clientManager.setCreateUserId(user.getId().toString());
            clientManager.setCreateUserName(user.getEname());
            service.getDao().saveObject(clientManager);

            JsonOut("添加成功!", "gorupManagerNav");
        } else {
            JsonOut("添加失败!");
        }
    }

    public void delClientManager() {
        if (clientManager != null) {
            service.getDao().deleteObject(clientManager);
            JsonOut("删除成功!", "gorupManagerNav");
        } else {
            JsonOut("删除失败!");
        }
        //先删除该用户组旗下的所有菜单

        // service.getDao().deleteGorupResources(gorupManager.getId());
        //再删除该菜单
        //  service.getDao().delete(gorupManager.getId());
        // JsonOut("删除成功!", "gorupManagerNav");

    }

    public String cfgResources() {
        //获得所有的资源菜单
        //List<ORG_Resources> list = resService.getDao().listAll();

        //往request里放attribute
        //getRequest().setAttribute("list", list);

        //取得父节点
        //List<ORG_Resources> parentRes = new ArrayList<ORG_Resources>();
        //取得子节点
        // List<ORG_Resources> sonRes = new ArrayList<ORG_Resources>();
        // for (ORG_Resources res : list) {
        //     if (res.getParentId() == 0) {
        //         parentRes.add(res);
        //     } else {
        //         sonRes.add(res);
        //    }
        // }

        //取得用户组对象
        // gorupManager = service.getDao().get(gorupManager.getId());

        //取得用户组已有权限列表
        //List<ORG_GorupResources> resList = service.getDao().getGorupResources(gorupManager.getId());

        //取得该用户组旗下的员工列表
        // List<User> userList = service.getDao().getUserByGorup(gorupManager.getId());

        //getRequest().setAttribute("gorupManager", gorupManager);
        //getRequest().setAttribute("userList", userList);
        //getRequest().setAttribute("resList", resList);
        // getRequest().setAttribute("parentRes", parentRes);
        //getRequest().setAttribute("sonRes", sonRes);
        return "cfgResources";
    }


    public void editResources() {

        //1.先删除该用户组旗下的所有菜单


        /*   if (resList != null && resList.length > 0) {
            service.getDao().deleteGorupResources(gorupManager.getId());
        }

        //2.循环针对当前菜单权限
        for (String str : resList) {
            ORG_GorupResources gRes = new ORG_GorupResources();
            gRes.setCreateDate(new Date());
            gRes.setResourcesId(Integer.parseInt(str));
            gRes.setGorupManagerId(gorupManager.getId());

            //保存对象
            service.getDao().saveObject(gRes);
        }

        JSONObject object = new JSONObject();


        object.put("statusCode", "200");
        object.put("message", "修改成功");
        object.put("navTabId", "");
        object.put("rel", "");
        object.put("callbackType", "");
        object.put("forwardUrl", "");
        out(object);*/
    }

}
